<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "pay_rebate_usertake".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $audit_uid
 * @property integer $give_uid
 * @property string $take_money
 * @property integer $state
 * @property integer $apply_time
 * @property integer $ok_time
 * @property integer $take_time
 * @property integer $err_time
 * @property string $memo
 */
class PayRebateUsertake extends \yii\db\ActiveRecord
{
    public function attributes()
    {
        // add related fields to searchable attributes
        return array_merge(parent::attributes(), ['tel','show_money']);
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'pay_rebate_usertake';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'audit_uid', 'give_uid', 'state', 'apply_time', 'ok_time', 'take_time', 'err_time'], 'integer'],
            [['take_money'], 'number'],
            [['memo'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => '用户ID',
            'audit_uid' => '审核uid',
            'give_uid' => '打款uid',
            'take_money' => '提现金额',
            'state' => '状态',
            'apply_time' => '申请时间',
            'ok_time' => '审核通过时间',
            'take_time' => '打钱时间',
            'err_time' => '审核失败时间',
            'memo' => '备注',
            'tel'=>'电话'
        ];
    }
    public static function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理，如果有的话
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        
        //cert 与 key 分别属于两个.pem文件   yii::$app->params['weixin']['cert_path']
        //服务器   /home/www/gjj/vendor/WXPay/cert/apiclient_cert.pem
        // home/www/gjj/vendor/WXPay/cert/apiclient_key.pem
        curl_setopt($ch,CURLOPT_SSLCERT,yii::$app->params['weixin']['cert_path']);
        curl_setopt($ch,CURLOPT_SSLKEY,yii::$app->params['weixin']['key_path']);  
        if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
        $data = curl_exec($ch);
        //$data=curl_getinfo($ch);
        if($data){
            curl_close($ch);
            return $data;
        }
        else {
            $error = curl_errno($ch);
            curl_close($ch);
            return $error;
        }
    }
    /**
     * 获取两位随机字母
     */
    public static  function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;
        
        for($i=0;$i<$length;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }
        
        return $str;
    }
    public static function secretkeyAction($package) {
        $ApikeySettle = Yii::$app->params['weixin']['WX_KEY'];
        ksort($package, SORT_STRING);
        $string1 = '';
        foreach($package as $key => $v) {
            if (empty($v)) {
                continue;
            }
            $string1 .= "{$key}={$v}&";
        }
        $string1 .= "key={$ApikeySettle}";
        $package['sign'] = strtoupper(md5($string1));
        return $package;
    }
    
    /**
     * 数组转xml
     * @param unknown $arr
     * @param number $level
     * @return Ambigous <string, unknown>
     */
   public static function array2xml($arr, $level = 1) {
        $s = $level == 1 ? "<xml>" : '';
        foreach ($arr as $tagname => $value) {
            if (is_numeric($tagname)) {
                $tagname = $value['TagName'];
                unset($value['TagName']);
            }
            if (!is_array($value)) {
                $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
            } else {
                $s .= "<{$tagname}>" . array2xml($value, $level + 1) . "</{$tagname}>";
            }
        }
        $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
        return $level == 1 ? $s . "</xml>" : $s;
    }
}
